package com.example.xinli.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xinli.service.IInterService;
import com.example.xinli.utils.TokenUtil;
import com.example.xinli.domain.vo.InterSubmitVo;
import com.example.xinli.domain.vo.ResponseBean;
//import jdk.nashorn.internal.parser.Token;
import com.example.xinli.domain.vo.InterStartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/inter")
public class InterController {

    @Autowired
    private IInterService interService;

    // 获取干预信息
    @RequestMapping("/getInter")
    @ResponseBody
    public ResponseBean getInter(@RequestParam("interId") int interId) {
        return interService.getInterInfo(interId);
    }

    // 获取一次干预中所有的页面
    @RequestMapping("/getAllInterPages")
    @ResponseBody
    public ResponseBean getAllInterPages(@RequestParam("interId") int interId) {
        return interService.getAllInterPages(interId);
    }

    // 获取一次干预中的某个页面
    @RequestMapping("/getInterPageById")
    @ResponseBody
    public ResponseBean getInterPageById(@RequestParam("interId") int interId, @RequestParam("pageId") int pageId) {
        return interService.getInterPageById(interId, pageId);
    }

    // 获取用户的所有干预
    @RequestMapping("/getAllUserInter")
    @ResponseBody
    public ResponseBean getAllUserInter(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = TokenUtil.getId(token);
        return interService.getAllUserInter(userId);
    }

    // 获取用户的所有已完成干预
    @RequestMapping("/getFinishedUserInter")
    @ResponseBody
    public ResponseBean getFinishedUserInter(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        return interService.getFinishedUserInter(userId);
    }

    // 获取用户的所有未完成干预
    @RequestMapping("/getUnfinishedUserInter")
    @ResponseBody
    public ResponseBean getUnfinishedUserInter(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        return interService.getUnfinishedUserInter(userId);
    }

    // 用户开始干预
    @RequestMapping("/startInter")
    @ResponseBody
    public ResponseBean startInter(HttpServletRequest request, @RequestBody InterStartVo interStartVo) throws Exception {

        try {
            // 调用服务
            return interService.startInter(interStartVo);

        } catch (Exception e) {
            // 捕获其他异常
            ResponseBean errorResponse = new ResponseBean();
            errorResponse.setCode(500);
            errorResponse.setMessage("系统内部错误：" + e.getMessage());
            return errorResponse;
        }


    }

    // 用户完成干预后 提交结果
    @RequestMapping("/submitInter")
    @ResponseBody
    public ResponseBean submitInter(HttpServletRequest request, @RequestBody InterSubmitVo interSubmitVo) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        interSubmitVo.setUserId(userId);

        return interService.submitInter(interSubmitVo);
    }

    @RequestMapping("/checkInterAvailability")
    @ResponseBody
    public ResponseBean checkInterAvailability(HttpServletRequest request, @RequestParam("interId") int interId) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        return interService.checkInterAvailability(userId, interId);
    }

    @RequestMapping("/getUserGoal")
    @ResponseBody
    public ResponseBean getUserGoal(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userId = TokenUtil.getId(token);
        return interService.getUserGoal(userId);
    }
}

